#!/bin/bash

# Packages
#   vorbis-tools
#   cdlabelgen
#   imagemagick
#   any package you use to view pdf

VIEWER=xreader

clear

# test input parameter
if [ "$1" != "" ]; then
  cd $1
fi

# switch
y=0

# generate song list
for i in *.ogg
  do
    if [ $y -eq 0 ]
    then
      y=1
      title=`ogginfo "$i" | grep ALBUM= | cut -d \= -f 2`
      artist=`ogginfo "$i" | grep ALBUMARTIST= | cut -d \= -f 2`
      year=`ogginfo "$i" | grep DATE= | cut -b 7,8,9,10`
      echo '{#MB}'$title >> sl-utf-8.txt
      echo '{#M}'$artist  >> sl-utf-8.txt
      echo '{#M}'$year  >> sl-utf-8.txt
      echo ' ' >> sl-utf-8.txt
    fi
    song_name=`ogginfo "$i" | grep TITLE | cut -d \= -f 2`
    track_number=`ogginfo "$i" | grep TRACKNUMBER | cut -d \= -f 2`
    echo $track_number $song_name >> sl-utf-8.txt
  done

# convert list from utf-8 to ascii
iconv -f UTF-8 -t ASCII//TRANSLIT sl-utf-8.txt > sl-ascii.txt

# convert cover image to eps
convert cover.* cover.eps

# generate cd cover
cdlabelgen -D -m -w -f sl-ascii.txt -e cover.eps -S 0 -o cd_cover.ps

# convert cd cover to pdf
ps2pdf cd_cover.ps cd_cover.pdf

# view it
$VIEWER cd_cover.pdf

# delete temporary files
rm sl-ascii.txt
rm sl-utf-8.txt
rm cover.eps
rm cd_cover.ps

exit
